﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.


using System;
using System.Reflection;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Runtime.InteropServices;

using Microsoft.Office.Interop.Outlook;

namespace Ksamuelsen.Office.Outlook {
    [ComVisible(true)]
    public partial class OutlookPropertyPage : UserControl, PropertyPage {
        public delegate bool ValidatorDelegate(Control ctrl, string value);

        private string           _title;
        private string           _helpfile;
        private int              _helpfilecontext;
        private bool             _dirty = false;
        private PropertyPageSite _site;
        private bool             _loaded = false;
        private bool             _loading = false;

        public OutlookPropertyPage() : this(String.Empty, String.Empty, 0) {
        }

        public OutlookPropertyPage(string title, string helpfile, int helpfilecontext) {
            InitializeComponent();

            _title           = title;
            _helpfile        = helpfile;
            _helpfilecontext = helpfilecontext;
        }

        public void AddValidator(Control ctrl, ValidatorDelegate validateDelegate, string validateMessage) {
            _errorProvider.SetIconPadding(ctrl, 5);
            ctrl.TextChanged += delegate(object sender, EventArgs e) {
                if ((sender != null) && (sender is Control)) {
                    Control c = sender as Control;
                    if (!validateDelegate(c, c.Text)) {
                        _errorProvider.SetError(c, validateMessage);
                    } else {
                        _errorProvider.SetError(c, "");
                    }
                    
                }
            
            };
                
        }

        public bool Dirty {
            get {
                return _dirty;
            }
            set {
                _dirty = value;
                if (!_loading)
                    NotifyStatusChange();
            }
        }

        public bool CheckError(Control ctrl) {
            return (!String.IsNullOrEmpty(_errorProvider.GetError(ctrl)));
        }

        public string GetError(Control ctrl) {
            return _errorProvider.GetError(ctrl);
        }


        public void Apply() {
            OnSaveSettings();
            _dirty = false;
        }


        public void GetPageInfo(ref string helpfile, ref int helpcontext) {
            helpfile = _helpfile;
            helpcontext = _helpfilecontext;
        }


        protected virtual void OnLoadSettings() {
        }

        protected virtual void OnSaveSettings() {
            _dirty = false;
        }

        protected void NotifyStatusChange() {
            if (PropertyPageSite != null)
                PropertyPageSite.OnStatusChange();

        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            if (!_loaded) {
                _loading = true;
                OnLoadSettings();
                _loading = false;
                _dirty = false;
                _loaded = true;
            }
        }


        protected PropertyPageSite PropertyPageSite {
            get {
                if (_site == null) {
                    try {
                        Assembly swf = Assembly.GetAssembly(typeof(UserControl));
                        Type method = swf.GetType("System.Windows.Forms.UnsafeNativeMethods");
                        Type oleobj = method.GetNestedType("IOleObject");
                        MethodInfo clientSite = oleobj.GetMethod("GetClientSite");

                        _site = (PropertyPageSite)clientSite.Invoke(this, null);
                    } catch (System.Exception e) {
                        System.Diagnostics.Debug.WriteLine(e.ToString());
                    }
                }
                return _site;
            }
        }

        public static void AddPage(PropertyPages pages, OutlookPropertyPage page) {
            pages.Add(page, String.Empty);
        }




    }
}
